package com.shouhuobao.take.mine;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;
import com.shouhuobao.take.model.TakeWhetherChargesBean;

import droid.frame.ui.utils.Utils;

/**
 * 有偿代收服务
 * Created by lidandan on 2018/2/2.
 */

public class TakePaidFragment extends BaseFragment implements View.OnClickListener {
    private LinearLayout mCurierPayLL;
    private CheckBox mCurierpPayCB;
    private LinearLayout mUserPayLL;
    private CheckBox mUserPayCB;
    private CheckBox mOnlineCB;
    private LinearLayout mCashLinELL;
    private CheckBox mCashLineCB;
    private TextView mSmallBagBt1, mSmallBagBt2, mSmallBagBt3, mSmallBagBt4;
    private TextView mBigBagBt1, mBigBagBt2, mBigBagBt3, mBigBagBt4;
    private TextView mOvertimeSmallBagBt1, mOvertimeSmallBagBt2;
    private TextView mOvertimeBigBagBt1, mOvertimeBigBagBt2;
    private LinearLayout mOvertimeLayout;

    private TakeWhetherChargesBean item = null;
    private boolean isCashPay = false;
    private double smallPrice = 0.5;
    private double bigPrice = 1;
    private double timeOutSmallPrice = 0;
    private double timeOutBigPrice = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            item = (TakeWhetherChargesBean) bundle.getSerializable("item");
        }
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.take_paid_fragment);
        super.findViewById();
        mCurierPayLL = (LinearLayout) findViewById(R.id.curier_pay_ll);
        mCurierpPayCB = (CheckBox) findViewById(R.id.curier_pay_cb);
        mUserPayLL = (LinearLayout) findViewById(R.id.user_pay_ll);
        mUserPayCB = (CheckBox) findViewById(R.id.user_pay_cb);
        mOnlineCB = (CheckBox) findViewById(R.id.online_cb);
        mCashLinELL = (LinearLayout) findViewById(R.id.cash_line_ll);
        mCashLineCB = (CheckBox) findViewById(R.id.cash_line_cb);
        mSmallBagBt1 = (TextView) findViewById(R.id.small_bag_bt1);
        mSmallBagBt2 = (TextView) findViewById(R.id.small_bag_bt2);
        mSmallBagBt3 = (TextView) findViewById(R.id.small_bag_bt3);
        mSmallBagBt4 = (TextView) findViewById(R.id.small_bag_bt4);
        mBigBagBt1 = (TextView) findViewById(R.id.big_bag_bt1);
        mBigBagBt2 = (TextView) findViewById(R.id.big_bag_bt2);
        mBigBagBt3 = (TextView) findViewById(R.id.big_bag_bt3);
        mBigBagBt4 = (TextView) findViewById(R.id.big_bag_bt4);
        mOvertimeLayout = (LinearLayout) findViewById(R.id.overtime_layout);
        mOvertimeSmallBagBt1 = (TextView) findViewById(R.id.overtime_small_bag_bt1);
        mOvertimeSmallBagBt2 = (TextView) findViewById(R.id.overtime_small_bag_bt2);
        mOvertimeBigBagBt1 = (TextView) findViewById(R.id.overtime_big_bag_bt1);
        mOvertimeBigBagBt2 = (TextView) findViewById(R.id.overtime_big_bag_bt2);
        mCurierPayLL.setOnClickListener(this);
        mCurierpPayCB.setOnClickListener(this);
        mUserPayLL.setOnClickListener(this);
        mUserPayCB.setOnClickListener(this);
        mCashLinELL.setOnClickListener(this);
        mCashLineCB.setOnClickListener(this);
        mSmallBagBt1.setOnClickListener(this);
        mSmallBagBt2.setOnClickListener(this);
        mSmallBagBt3.setOnClickListener(this);
        mSmallBagBt4.setOnClickListener(this);
        mBigBagBt1.setOnClickListener(this);
        mBigBagBt2.setOnClickListener(this);
        mBigBagBt3.setOnClickListener(this);
        mBigBagBt4.setOnClickListener(this);
        mOvertimeSmallBagBt1.setOnClickListener(this);
        mOvertimeSmallBagBt2.setOnClickListener(this);
        mOvertimeBigBagBt1.setOnClickListener(this);
        mOvertimeBigBagBt2.setOnClickListener(this);
        mCurierpPayCB.setChecked(true);
        initView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //向快递员收费
            case R.id.curier_pay_ll:
            case R.id.curier_pay_cb:
                mCurierpPayCB.setChecked(true);
                mUserPayCB.setChecked(false);
                mOvertimeLayout.setVisibility(View.GONE);
                break;
            //向用户收费
            case R.id.user_pay_ll:
            case R.id.user_pay_cb:
                mCurierpPayCB.setChecked(false);
                mUserPayCB.setChecked(true);
                mOvertimeLayout.setVisibility(View.VISIBLE);
                break;
            //现金支付
            case R.id.cash_line_ll:
            case R.id.cash_line_cb:
                if (isCashPay) {
                    isCashPay = false;
                } else {
                    isCashPay = true;
                }
                mCashLineCB.setChecked(isCashPay);
                break;
            //小件包裹 0.3
            case R.id.small_bag_bt1:
                setSmallParcelMoney(0.3);
                break;
            //小件包裹 0.5
            case R.id.small_bag_bt2:
                setSmallParcelMoney(0.5);
                break;
            //小件包裹 1
            case R.id.small_bag_bt3:
                setSmallParcelMoney(1);
                break;
            //小件包裹  自定义
            case R.id.small_bag_bt4:
                DialogMgr.showTakeSetPayMoney((BaseActivity2) getActivityContext(), 1, 3);
                break;
            //大件包裹 0.5
            case R.id.big_bag_bt1:
                setBigParcelMoney(0.5);
                break;
            //大件包裹 1
            case R.id.big_bag_bt2:
                setBigParcelMoney(1);
                break;
            //大件包裹 2
            case R.id.big_bag_bt3:
                setBigParcelMoney(2);
                break;
            //大件包裹 自定义
            case R.id.big_bag_bt4:
                DialogMgr.showTakeSetPayMoney((BaseActivity2) getActivityContext(), 2, 5);
                break;
            //超期小件包裹 免费
            case R.id.overtime_small_bag_bt1:
                setTimeOutSmallParcelMoney(0);
                break;
            //超期小件包裹 0.3
            case R.id.overtime_small_bag_bt2:
                setTimeOutSmallParcelMoney(0.3);
                break;
            //超期大件包裹 免费
            case R.id.overtime_big_bag_bt1:
                setTimeOutBigParcelMoney(0);
                break;
            //超期大件包裹 0.5
            case R.id.overtime_big_bag_bt2:
                setTimeOutBigParcelMoney(0.5);
                break;
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //获取设置的收费信息
            case MsgID.get_take_paid:
                final ReqResult<TakeWhetherChargesBean> result = JSON.parser(msg.obj, TakeWhetherChargesBean.class);
                if (checkLoginStatus(result)) {
                    getActivityContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            item = result.getResult();
                            initView();
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return false;
            //提交收费信息
            case MsgID.submit_take_paid_info:
                cancelLoadingDialog();
                ReqResult<?> reqResult = JSON.parser(msg.obj);
                if (checkLoginStatus(reqResult)) {
                    showToast("收费设置成功，具体收费功能即将上线，敬请期待！");
                    getActivityContext().finish();
                } else {
                    showToast(reqResult.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    /**
     * 初始化布局数据
     */
    private void initView() {
        //默认状态
        if (item == null) {
            mCurierpPayCB.setChecked(true);
            mUserPayCB.setChecked(false);
            mOnlineCB.setChecked(true);
            mCashLineCB.setChecked(isCashPay);
            mSmallBagBt2.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mSmallBagBt2.setTextColor(Color.parseColor("#FDD120"));
            mBigBagBt2.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mBigBagBt2.setTextColor(Color.parseColor("#FDD120"));
            mOvertimeLayout.setVisibility(View.GONE);
        } else {
            //收费对象
            if (item.getChargeObject() == 2) {
                mCurierpPayCB.setChecked(false);
                mUserPayCB.setChecked(true);
                mOvertimeLayout.setVisibility(View.VISIBLE);
            } else {
                mCurierpPayCB.setChecked(true);
                mUserPayCB.setChecked(false);
                mOvertimeLayout.setVisibility(View.GONE);
            }
            //支付方式
            if (item.getPayType() == 4) {
                mOnlineCB.setChecked(true);
                mCashLineCB.setChecked(true);
            } else {
                mOnlineCB.setChecked(true);
                mCashLineCB.setChecked(false);
            }
            //小件包裹
            setSmallParcelMoney(item.getSmallParcel());
            //大件包裹
            setBigParcelMoney(item.getBigParcel());
            if (item.getChargeObject() == 2) {
                //小件包裹
                setTimeOutSmallParcelMoney(item.getTimeoutSmallParcel());
                //大件包
                //大件包裹
                setTimeOutBigParcelMoney(item.getTimeoutBigParcel());
            }

        }
    }

    /**
     * 设置小包裹选中布局
     *
     * @param money
     */
    private void setSmallParcelMoney(double money) {
        if (money == 0.3) {
            smallPrice = 0.3;
            mSmallBagBt1.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mSmallBagBt1.setTextColor(Color.parseColor("#FDD120"));
            mSmallBagBt1.setText(money + "元");
            mSmallBagBt2.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mSmallBagBt2.setTextColor(Color.parseColor("#6F8FAF"));
            mSmallBagBt3.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mSmallBagBt3.setTextColor(Color.parseColor("#6F8FAF"));
            mSmallBagBt4.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mSmallBagBt4.setTextColor(Color.parseColor("#6F8FAF"));
        } else if (money == 0.5) {
            smallPrice = 0.5;
            mSmallBagBt1.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mSmallBagBt1.setTextColor(Color.parseColor("#6F8FAF"));
            mSmallBagBt2.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mSmallBagBt2.setTextColor(Color.parseColor("#FDD120"));
            mSmallBagBt2.setText(money + "元(推荐)");
            mSmallBagBt3.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mSmallBagBt3.setTextColor(Color.parseColor("#6F8FAF"));
            mSmallBagBt4.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mSmallBagBt4.setTextColor(Color.parseColor("#6F8FAF"));
        } else if (money == 1) {
            smallPrice = 1;
            mSmallBagBt1.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mSmallBagBt1.setTextColor(Color.parseColor("#6F8FAF"));
            mSmallBagBt2.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mSmallBagBt2.setTextColor(Color.parseColor("#6F8FAF"));
            mSmallBagBt3.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mSmallBagBt3.setTextColor(Color.parseColor("#FDD120"));
            mSmallBagBt3.setText(money + "元");
            mSmallBagBt4.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mSmallBagBt4.setTextColor(Color.parseColor("#6F8FAF"));
        } else {
            smallPrice = money;
            mSmallBagBt1.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mSmallBagBt1.setTextColor(Color.parseColor("#6F8FAF"));
            mSmallBagBt2.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mSmallBagBt2.setTextColor(Color.parseColor("#6F8FAF"));
            mSmallBagBt3.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mSmallBagBt3.setTextColor(Color.parseColor("#6F8FAF"));
            mSmallBagBt4.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mSmallBagBt4.setTextColor(Color.parseColor("#FDD120"));
            mSmallBagBt4.setText(money + "元");
        }
        mSmallBagBt1.setPadding(Utils.dpToPx(16, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(20, getResources()), Utils.dpToPx(5, getResources()));
        mSmallBagBt2.setPadding(Utils.dpToPx(16, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(20, getResources()), Utils.dpToPx(5, getResources()));
        mSmallBagBt3.setPadding(Utils.dpToPx(16, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(20, getResources()), Utils.dpToPx(5, getResources()));
        mSmallBagBt4.setPadding(Utils.dpToPx(16, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(20, getResources()), Utils.dpToPx(5, getResources()));
    }

    /**
     * 设置大包裹选择布局
     *
     * @param money
     */
    private void setBigParcelMoney(double money) {
        if (money == 0.5) {
            bigPrice = 0.5;
            mBigBagBt1.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mBigBagBt1.setTextColor(Color.parseColor("#FDD120"));
            mBigBagBt1.setText(money + "元");
            mBigBagBt2.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mBigBagBt2.setTextColor(Color.parseColor("#6F8FAF"));
            mBigBagBt3.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mBigBagBt3.setTextColor(Color.parseColor("#6F8FAF"));
            mBigBagBt4.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mBigBagBt4.setTextColor(Color.parseColor("#6F8FAF"));
        } else if (money == 1) {
            bigPrice = 1;
            mBigBagBt1.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mBigBagBt1.setTextColor(Color.parseColor("#6F8FAF"));
            mBigBagBt2.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mBigBagBt2.setTextColor(Color.parseColor("#FDD120"));
            mBigBagBt2.setText(money + "元(推荐)");
            mBigBagBt3.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mBigBagBt3.setTextColor(Color.parseColor("#6F8FAF"));
            mBigBagBt4.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mBigBagBt4.setTextColor(Color.parseColor("#6F8FAF"));
        } else if (money == 2) {
            bigPrice = 2;
            mBigBagBt1.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mBigBagBt1.setTextColor(Color.parseColor("#6F8FAF"));
            mBigBagBt2.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mBigBagBt2.setTextColor(Color.parseColor("#6F8FAF"));
            mBigBagBt3.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mBigBagBt3.setTextColor(Color.parseColor("#FDD120"));
            mBigBagBt3.setText(money + "元");
            mBigBagBt4.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mBigBagBt4.setTextColor(Color.parseColor("#6F8FAF"));
        } else {
            bigPrice = money;
            mBigBagBt1.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mBigBagBt1.setTextColor(Color.parseColor("#6F8FAF"));
            mBigBagBt2.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mBigBagBt2.setTextColor(Color.parseColor("#6F8FAF"));
            mBigBagBt3.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mBigBagBt3.setTextColor(Color.parseColor("#6F8FAF"));
            mBigBagBt4.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mBigBagBt4.setTextColor(Color.parseColor("#FDD120"));
            mBigBagBt4.setText(money + "元");
        }
        mBigBagBt1.setPadding(Utils.dpToPx(16, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(20, getResources()), Utils.dpToPx(5, getResources()));
        mBigBagBt2.setPadding(Utils.dpToPx(16, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(20, getResources()), Utils.dpToPx(5, getResources()));
        mBigBagBt3.setPadding(Utils.dpToPx(16, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(20, getResources()), Utils.dpToPx(5, getResources()));
        mBigBagBt4.setPadding(Utils.dpToPx(16, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(20, getResources()), Utils.dpToPx(5, getResources()));
    }

    /**
     * 设置超时小件包裹价钱
     *
     * @param money
     */
    private void setTimeOutSmallParcelMoney(double money) {
        if (money == 0) {
            timeOutSmallPrice = 0;
            mOvertimeSmallBagBt1.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mOvertimeSmallBagBt1.setTextColor(Color.parseColor("#FDD120"));
            mOvertimeSmallBagBt1.setText("不收费");
            mOvertimeSmallBagBt2.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mOvertimeSmallBagBt2.setTextColor(Color.parseColor("#6F8FAF"));
        } else if (money == 0.3) {
            timeOutSmallPrice = 0.3;
            mOvertimeSmallBagBt1.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mOvertimeSmallBagBt1.setTextColor(Color.parseColor("#6F8FAF"));
            mOvertimeSmallBagBt2.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mOvertimeSmallBagBt2.setTextColor(Color.parseColor("#FDD120"));
            mOvertimeSmallBagBt2.setText(money + "元");
        }
        mOvertimeSmallBagBt1.setPadding(Utils.dpToPx(16, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(20, getResources()), Utils.dpToPx(5, getResources()));
        mOvertimeSmallBagBt2.setPadding(Utils.dpToPx(16, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(20, getResources()), Utils.dpToPx(5, getResources()));
    }

    /**
     * 设置超时大件包裹价钱
     *
     * @param money
     */
    private void setTimeOutBigParcelMoney(double money) {
        if (money == 0) {
            timeOutBigPrice = 0;
            mOvertimeBigBagBt1.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mOvertimeBigBagBt1.setTextColor(Color.parseColor("#FDD120"));
            mOvertimeBigBagBt1.setText("不收费");
            mOvertimeBigBagBt2.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mOvertimeBigBagBt2.setTextColor(Color.parseColor("#6F8FAF"));
        } else if (money == 0.5) {
            timeOutBigPrice = 0.5;
            mOvertimeBigBagBt1.setBackgroundResource(R.drawable.task_contact_sender_phone_6f8faf_bg);
            mOvertimeBigBagBt1.setTextColor(Color.parseColor("#6F8FAF"));
            mOvertimeBigBagBt2.setBackgroundResource(R.drawable.take_paid_6f8faf);
            mOvertimeBigBagBt2.setTextColor(Color.parseColor("#FDD120"));
            mOvertimeBigBagBt2.setText(money + "元");
        }
        mOvertimeBigBagBt1.setPadding(Utils.dpToPx(16, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(20, getResources()), Utils.dpToPx(5, getResources()));
        mOvertimeBigBagBt2.setPadding(Utils.dpToPx(16, getResources()), Utils.dpToPx(5, getResources()), Utils.dpToPx(20, getResources()), Utils.dpToPx(5, getResources()));
    }

    /**
     * 设置小包或大包自定义的金额
     */
    public void setSmallOrBigPrice(double money, int type) {
        if (money == 0) {
            return;
        }
        if (type == 1) {
            setSmallParcelMoney(money);
        } else if (type == 2) {
            setBigParcelMoney(money);
        }
    }

    /**
     * @param chargeType 1 免费 2 收费
     */
    public void submitInfo(int chargeType) {
        if (item == null || item.isChargeFlag()) {
            showLoadingDialog(null);
            AppHttp.getInstance().submitTakePaidInfo(chargeType, mUserPayCB.isChecked() ? 2 : 1, mCashLineCB.isChecked() ? 4 : 1, smallPrice, bigPrice, timeOutSmallPrice, timeOutBigPrice);
        } else {
            DialogMgr.showNotSubmitCostDialog(getActivityContext());
        }
    }
}
